package net.hn.hnms.biz.emgcy.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.hn.hnms.biz.emgcy.domain.ExpertPo;
import net.hn.hnms.biz.emgcy.domain.dto.ExpertDto;
import net.hn.hnms.biz.emgcy.domain.vo.ExpertVo;
import net.hn.hnms.biz.emgcy.mapper.ExpertMapper;
import net.hn.hnms.biz.emgcy.service.ExpertService;
import net.hn.hnms.biz.emgcy.util.FileUtil;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.system.api.RemoteDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: baijiajun
 * @Date: 2022/9/13
 */
@Service
public class ExpertServiceImpl extends ServiceImpl<ExpertMapper, ExpertPo> implements ExpertService {
    @Resource
    private ExpertMapper expertMapper;
    @Autowired
    private RemoteDictService remoteDictService;

    @Override
    public ResponsePage<ExpertVo> pageList(PageQuery pageQuery, ExpertDto expertDto) {
        pageQuery.setPageNum(pageQuery.getPageNum() >= 1 ? (pageQuery.getPageNum() - 1) * pageQuery.getPageSize() : 0);
        List<ExpertVo> list = expertMapper.pageList(pageQuery.getPageSize(), pageQuery.getPageNum(), expertDto);
        if (list.isEmpty()) {
            return new ResponsePage<>(new ArrayList(), 0);
        }
        return new ResponsePage<>(list, list.get(0).getTotal());

    }

    @Override
    public Boolean add(ExpertPo expertPo) {
        int insert = expertMapper.insert(expertPo);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean update(ExpertPo expertPo) {
        int update = expertMapper.updateById(expertPo);
        if (update > 0) {
            return true;
        }
        return false;

    }

    @Override
    public Boolean delete(Long id) {
        int delete = expertMapper.deleteById(id);
        if (delete > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void export(HttpServletResponse httpServletResponse) throws IOException {
        List<ExpertPo> list = expertMapper.selectList(null);
        List<Map<String, Object>> listExport = new ArrayList<>();
        list.forEach(item -> {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("专家姓名", item.getName());
            if (StringUtils.isNotEmpty(item.getType())) {
                String label = remoteDictService.selectDictLabel("expert_type", item.getType());
                if (StringUtils.isNotEmpty(label)) {
                    map.put("专业领域", label);
                } else {
                    map.put("专业领域", null);
                }
            } else {
                map.put("专业领域", null);
            }
            map.put("联系电话", item.getTel());
            map.put("工作地址", item.getAddress());
            map.put("所属单位", item.getUnit());
            map.put("职责", item.getSpeciality());
            map.put("专家简介", item.getEmResume());
            /*if(StringUtils.isNotEmpty(item.getQualificationCode())){
                String label1 = remoteDictService.selectDictLabel("qualification_code", item.getQualificationCode());
                if(StringUtils.isNotEmpty(label1)){
                    map.put("最高学历",label1);
                }else{
                    map.put("最高学历",null);
                }
            }else{
                map.put("最高学历",null);
            }*/
            /*map.put("职称",item.getProTitle());

            map.put("毕业院校",item.getGraduate());
            map.put("邮箱",item.getEmail());*/


            listExport.add(map);
        });
        FileUtil.downloadExcel(listExport, httpServletResponse);
    }
}
